package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.application.MyApplication;
import com.example.myapplication.service.DownloadService;
import com.example.myapplication.util.HttpClientUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DownloadActivity extends AppCompatActivity implements View.OnClickListener {

    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);
        textView = findViewById(R.id.t);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(()->{
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    textView.setText("子线程修改UI12121!");
                }).start();
            }
        });
        method();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public void method(){
        Thread t = new Thread() {
            public void run() {
                try {
                    //html = HttpClientUtils.getHtml("https://v1.hitokoto.cn/?c=f&encode=text"); // 获取网页源码 get请求
                    String html = HttpClientUtils.useHttpUrlConnectionGet("http://10.0.2.2:8082/student/list"); //get请求
                    Thread.sleep(1000);
                    textView.setText(html);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();
    }
    @Override
    public void onClick(View view) {
        MyApplication myApplication1=(MyApplication) getApplication();
        Toast.makeText(this, ""+ MyApplication.getName(), Toast.LENGTH_SHORT).show();
       /* List<String> list = new ArrayList<>();
        list.add( "Android通过startService播放背景音乐;http://www.jb51.net/article/76479.htm" );
        Iterator iterator = list.iterator();
        String str = (String)iterator.next();
        String[] splits = str.split( ";" );
        String name = splits[ 0 ];
        String url = splits[ 1 ];
        Intent intent = new Intent( this , DownloadService.class );
        intent.putExtra( "name" , "Android通过startService播放背景音乐");
        intent.putExtra( "url" , "http://www.jb51.net/article/76479.htm");
        //启动IntentService
        startService(intent);*/
    }

    /**
     * ViewRootImpl的创建在onResume方法回调之后，而我们一开篇是在onCreate方法中创建了子线程并访问UI，在那个时刻，ViewRootImpl是没有创建的，无法检测当前线程是否是UI线程，
     * 所以程序没有崩溃一样能跑起来，而之后修改了程序，让线程休眠了200毫秒后，程序就崩了。很明显200毫秒后ViewRootImpl已经创建了，可以执行checkThread方法检查当前线程
     */
}